//
//  ChoiceStoreViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/9/20.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "ChoiceStoreViewController.h"
#import "SearchNet.h"
#import "NSString+SortNameUtils.h"
#import "Partner.h"
#import <MBProgressHUD.h>
#import "BusinessTableViewCell.h"
#import "BLHeader.h"

@interface ChoiceStoreViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *partners;
@property (nonatomic, strong) NSArray *characters;
@property (nonatomic, strong) NSArray *grouplist;


@end

@implementation ChoiceStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self dataFromServer];
}

- (void)setupUI {
    self.title = @"选择店主";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"BusinessTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
}

- (void)dataFromServer {
     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
     hud.mode = MBProgressHUDModeIndeterminate;
    [SearchNet saleOrStoreList:^(id result, NSError *error) {
        if (result) {
            self.partners = result;
            // 重新分组
            [NSString groupAndGetSectionTitles:self.partners objName:^NSString *(id obj) {
                Partner *p = obj;
                return p.name;
            } complement:^(NSArray<NSString *> *sectionTitles, NSArray<NSArray *> *groupLists) {
                self.grouplist = groupLists;
                self.characters = sectionTitles;
            }];
            [self.tableView reloadData];
            [hud hideAnimated:YES];
        }else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = [error localizedDescription];
            [hud hideAnimated:YES afterDelay:2];
        }
    }];
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.characters;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.characters.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.characters[section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BusinessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify];
    NSArray *list = self.grouplist[indexPath.section];
    [cell setupCell:list[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.partners.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     NSArray *list = self.grouplist[indexPath.section];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"choiceStore" object:list[indexPath.row]];
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
